package csce315.group10.androidapp;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class AddPotentialStoryDialog extends Activity{
	/* private instance variables */
	public CustomApp appState;
	String prefer_cover;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.addpotentialstorydialog);
        appState = ((CustomApp)getApplicationContext());
        Spinner spinner = (Spinner) findViewById(R.id.Get_prefer_cover_dropdown);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.yes_no_idc, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new PreferCoverSelectedListener());
    }
    
    public void okButtonHandler(View v){
    	/* gather the inputed information */
    	 EditText story_name = (EditText) findViewById(R.id.Get_story_name_text);
    	 String story_name_text = story_name.getText().toString();
    	 EditText story_date = (EditText) findViewById(R.id.Get_story_date_text);
    	 String story_date_text = story_date.getText().toString();
    	 EditText story_time = (EditText) findViewById(R.id.Get_story_time_text);
    	 String story_time_text = story_time.getText().toString();
    	 EditText contact_name = (EditText) findViewById(R.id.Get_story_contact_name_text);
    	 String contact_name_text = contact_name.getText().toString();
         EditText contact_phone = (EditText) findViewById(R.id.Get_story_contact_phone_text);
         String contact_phone_text = contact_phone.getText().toString();
         EditText location = (EditText) findViewById(R.id.Get_story_location_text);
         String location_text = location.getText().toString();
         EditText notes = (EditText) findViewById(R.id.Get_story_notes_text);
         String notes_text = notes.getText().toString();
         /* check if story already exists */
         for(int i = 0; i < appState.GetPStories().size(); i++){
        	 if(appState.GetPStories().get(i).get_story_name().equals(story_name_text)){
        		 Intent data = new Intent();
                 data.putExtra("exists", true);
        		 setResult(Activity.RESULT_OK, data);
        		 finish();
        	 }
         }
         /* build a new intent to return the data */
         Intent data = new Intent();
         data.putExtra("exists", false);
         data.putExtra("story_name", story_name_text);
         data.putExtra("story_date", story_date_text);
         data.putExtra("story_time", story_time_text);
         data.putExtra("contact_name", contact_name_text);
         data.putExtra("contact_phone", contact_phone_text);
         data.putExtra("prefer_cover", prefer_cover);
         data.putExtra("location", location_text);
         data.putExtra("notes", notes_text);
         setResult(Activity.RESULT_OK, data);
    	 finish();
    }
    
    /* class that monitors the staffer selector */
    public class PreferCoverSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        	if(parent.getItemAtPosition(pos).toString().equals("Yes")){
        		prefer_cover = appState.get_current_user().get_name();
        	}else if(parent.getItemAtPosition(pos).toString().equals("No")){
        		prefer_cover = "Anonymous";
        	}else{
        		prefer_cover = "Anyone";
        	}

        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
        
       
        
    }
    
    //need to be completed
    public void setSpinnerPosition(int pos){
    	
    }
    
    public void setSpinnerSelection(String select){
    	
    }
    
    public int getSpinnerPosition(){
    	return 0;
    }
    
    public String getSpinnerSelection(){
    	return null;
    }
}